﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Web.ks
{
    /// <summary>
    /// ksxy 的摘要说明
    /// </summary>
    public class ksxy : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string sn = string.IsNullOrEmpty(context.Request.Form["sn"]) ? context.Request.QueryString["sn"] : context.Request.Form["sn"];
            string zbj = string.IsNullOrEmpty(context.Request.Form["zbj"]) ? context.Request.QueryString["zbj"] : context.Request.Form["zbj"];
            string type = string.IsNullOrEmpty(context.Request.Form["type"]) ? context.Request.QueryString["type"] : context.Request.Form["type"];
            string returnstr = "请联系QQ:9021616 购买注册";
            if (type == "login")
            {
                //读取注册码
                returnstr = ReadFile(System.Web.HttpContext.Current.Server.MapPath("/live/ks/login/") + sn + ".html");
                if (string.IsNullOrEmpty(returnstr))
                {
                    returnstr = ReadFile(System.Web.HttpContext.Current.Server.MapPath("/TB/login/") + sn + ".html");
                }
            }
            else if (type == "wss")
            {
                returnstr = "query WebSocketInfoQuery($liveStreamId: String) {\n  webSocketInfo(liveStreamId: $liveStreamId) {\n    token\n    webSocketUrls\n    __typename\n  }\n}\n";
            }
            else if (type == "id")
            {
                returnstr = "query LiveDetail($principalId: String) {\n  webLiveDetail(principalId: $principalId) {\n    liveStream\n    feedInfo {\n      pullCycleMillis\n      __typename\n    }\n    watchingInfo {\n      likeCount\n      watchingCount\n      __typename\n    }\n    noticeList {\n      feed\n      options\n      __typename\n    }\n    fastComments\n    commentColors\n    moreRecommendList {\n      user {\n        id\n        avatar\n        name\n        __typename\n      }\n      watchingCount\n      poster\n      coverUrl\n      caption\n      id\n      playUrls {\n        quality\n        url\n        __typename\n      }\n      quality\n      gameInfo {\n        category\n        name\n        pubgSurvival\n        type\n        kingHero\n        __typename\n      }\n      hasRedPack\n      liveGuess\n      expTag\n      __typename\n    }\n    __typename\n  }\n}\n";
            }
            else if (type == "ksxy")
            {
                returnstr = "{}";
                JObject jo = JObject.Parse(returnstr);
                jo.Add("login", ReadFile(System.Web.HttpContext.Current.Server.MapPath("/live/ks/login/") + sn + ".html"));
                jo.Add("ld", "LiveDetail");
                jo.Add("ldq", "query LiveDetail($principalId: String) {\n  webLiveDetail(principalId: $principalId) {\n    liveStream\n    feedInfo {\n      pullCycleMillis\n      __typename\n    }\n    watchingInfo {\n      likeCount\n      watchingCount\n      __typename\n    }\n    noticeList {\n      feed\n      options\n      __typename\n    }\n    fastComments\n    commentColors\n    moreRecommendList {\n      user {\n        id\n        avatar\n        name\n        __typename\n      }\n      watchingCount\n      poster\n      coverUrl\n      caption\n      id\n      playUrls {\n        quality\n        url\n        __typename\n      }\n      quality\n      gameInfo {\n        category\n        name\n        pubgSurvival\n        type\n        kingHero\n        __typename\n      }\n      hasRedPack\n      liveGuess\n      expTag\n      __typename\n    }\n    __typename\n  }\n}\n");
                jo.Add("wss", "WebSocketInfoQuery");
                jo.Add("wssq", "query WebSocketInfoQuery($liveStreamId: String) {\n  webSocketInfo(liveStreamId: $liveStreamId) {\n    token\n    webSocketUrls\n    __typename\n  }\n}\n");
                jo.Add("share", "");
                jo.Add("sjs", randomNum(16) + "_" + LJSheng.Common.LCommon.GetTimeStamp(DateTime.Now));
                returnstr = JsonConvert.SerializeObject(jo, Formatting.Indented);
            }
            else
            {
                returnstr = ReadFile(System.Web.HttpContext.Current.Server.MapPath("/live/ks/login/") + sn + ".html");
                if (string.IsNullOrEmpty(returnstr))
                {
                    returnstr = ReadFile(System.Web.HttpContext.Current.Server.MapPath("/TB/login/") + sn + ".html");
                }
            }
            context.Response.Write(returnstr);
            context.Response.End();
        }
        public string randomNum(int length)
        {
            Random rdm = new Random();
            string text = string.Empty;
            string charset = "bjectSymhasOwnProp-0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgiklquvxz";

            for (int i = 0; i < length; i++)
            {
                text += charset[(int)Math.Ceiling(35 * rdm.NextDouble())];
            }
            return text;
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private string ReadFile(string path)
        {
            string s = "";
            if (File.Exists(path))
            {
                StreamReader myreader = null;
                try
                {
                    myreader = File.OpenText(path);
                    s = myreader.ReadToEnd();
                }
                catch
                {
                    s = null;
                }
                finally
                {
                    if (myreader != null)
                    {
                        myreader.Close();
                        myreader.Dispose();
                    }
                }
            }
            return s;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}